package io.renren.modules.forYoung.service.impl;

import io.renren.modules.forYoung.dao.ForYounyDao;
import io.renren.modules.forYoung.entity.ForYoungComment;
import io.renren.modules.forYoung.entity.ForYoungReply;
import io.renren.modules.forYoung.entity.ForYounyEntity;
import io.renren.modules.sys.entity.*;
import io.renren.modules.forYoung.service.ForYounyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ForYounyServiceImpl implements ForYounyService {

    @Autowired
    ForYounyDao younyDao;

    @Override
    public List getMyRecommendList(Map<String, Object> params) throws UnsupportedEncodingException {
        Integer count=younyDao.getForYoungCount(params);
        String num="";
        Integer limit= Integer.parseInt((String) params.get("limit"));
        for(int i=0;i<limit;i++){
            num= num+   (int)(1+Math.random()*(count-1+1))+",";
        }
        num=num.substring(0,num.length() - 1);
        params.put("num",num );
        List<ForYounyEntity> list=younyDao.getMyRecommendList(params);
        setImgList(list);
        return list;
    }

    @Override
    public Integer upload(Map<String, Object> param) {
        younyDao.insertFile(param);

        return younyDao.getNewImgId();
    }

    @Override
    public MysqlOssEntity getImgById(Integer id) {
        return younyDao.getImgById(id);
    }

    @Override
    public void saveData(ForYounyEntity forYounyEntity) {
        //将笔记数量+1
        younyDao.AddNote(forYounyEntity.getUserId());
        //保存笔记
        younyDao.saveData(forYounyEntity);

    }

    @Override
    public ForYounyEntity getForyoungById(Long forYoungId) {
        return younyDao.getForyoungById(Math.toIntExact(forYoungId));
    }


    @Override
    public Long savaForYoungComment(ForYoungComment forYoungComment) {
        younyDao.savaForYoungComment(forYoungComment);
        return younyDao.getNewCommentId();
    }

    @Override
    public void saveFirstComment(ForYounyEntity forYounyEntity) {
        younyDao.saveFirstComment(forYounyEntity);
    }

    @Override
    public ForYounyEntity getForYoungEntiyById(Integer id,Long userId) throws UnsupportedEncodingException {

        ForYounyEntity forYounyEntity= younyDao.getForYoungEntiyById(id,userId);
       List<ForYounyEntity> list=new ArrayList<>();
       list.add(forYounyEntity);
       setImgList(list);



        return forYounyEntity;
    }

    @Override
    public int toLike(Map params) {
        Integer l=younyDao.getLike(params);
        if(l==null){
             younyDao.AddLike(params);
            younyDao.insertLike(params);
            return 1;
        }else {
            younyDao.ReduceLike(params);
            younyDao.setNoLike(params);
            return 0;
        }

    }

    @Override
    public int toCollection(Map params) {
        Integer l=younyDao.getCollection(params);
        if(l==null){
            younyDao.AddCollection(params);
            younyDao.insertCollection(params);
            return 1;
        }else {
            younyDao.ReduceCollection(params);
            younyDao.setNoCollection(params);
            return 0;
        }
    }

    @Override
    public List<ForYounyEntity> getOurList(Map<String, Object> params) throws UnsupportedEncodingException {

        Integer count=younyDao.getForYoungCount(params);
        String num="";
        Integer limit= Integer.parseInt((String) params.get("limit"));
        for(int i=0;i<limit;i++){
            num= num+   (int)(1+Math.random()*(count-1+1))+",";
        }
        num=num.substring(0,num.length() - 1);
        params.put("num",num );
        List<ForYounyEntity> list=younyDao.getOurList(params);
        setImgList(list);
        return list;
    }

    @Override
    public List<ForYoungComment> getCommentList(Map<String, Object> params) throws UnsupportedEncodingException {
        List<ForYoungComment> list= younyDao.getCommentList(params);
        setCommentList(list);

        return list;
    }

    @Override
    public Integer saveReply(ForYoungReply forYoungReply) {
        younyDao.saveReply(forYoungReply);
        return younyDao.getNewReply();

    }

    @Override
    public ForYoungComment getCommentById(int commentId) {
        return younyDao.getCommentById(commentId);
    }

    @Override
    public void updateComment(ForYoungComment forYoungComment) {
        younyDao.updateComment(forYoungComment);
    }

    @Override
    public List<ForYoungReply> getReplyList(Map<String, Object> params) throws UnsupportedEncodingException {
        List<ForYoungReply> list= younyDao.getReplyList(params);
        setReglyList(list);
        return list;
    }

    @Override
    public int toCommentLike(Map params) {

        Integer l=younyDao.getCommentLike(params);
        if(l==null){
            younyDao.toCommentLike(params);
            return 1;
        }else {
            younyDao.setNoCommentLike(params);
            return 0;
        }
    }

    @Override
    public int toReplyLIke(Map params) {

        Integer l=younyDao.getReplyLIke(params);
        if(l==null){
            younyDao.toReplyLIke(params);
            return 1;
        }else {
            younyDao.setNoReplyLIke(params);
            return 0;
        }
    }

    @Override
    public List<ForYounyEntity> getOurFollowList(Map<String, Object> params) throws UnsupportedEncodingException {
        List<ForYounyEntity> list=younyDao.getOurFollowList(params);
        setImgList(list);
        return list;
    }

    @Override
    public List<ForYounyEntity> getListByDistance(Map<String, Object> params) throws UnsupportedEncodingException {
        List<ForYounyEntity> list=younyDao.getListByDistance(params);
        setImgList(list);
        return list;
    }

    @Override
    public List<ForYounyEntity> getMySchoolListByUserId(Map<String, Object> params) throws UnsupportedEncodingException {
        List<ForYounyEntity> list=younyDao.getMySchoolListByUserId(params);
        setImgList(list);
        return list;
    }

    @Override
    public MysqlOssEntity getImgByName(String name) {
        return younyDao.getImgByName(name);
    }

    @Override
    public List getMyStudyShare(Map map) {
        return younyDao.getMyStudyShare(map);
    }

    @Override
    public List getMyForYoung(Map map) throws UnsupportedEncodingException {
        List<ForYounyEntity> list= younyDao.getMyForYoung(map);
        setImgList(list);
        return list;
    }

    @Override
    public List getSeachList(Map<String, Object> params) throws UnsupportedEncodingException {
       List<ForYounyEntity>list= younyDao.getSeachList(params);
      setImgList(list);


        return list;
    }

    @Override
    public List getMyForYoungCollection(Map map) throws UnsupportedEncodingException {
       List list =  younyDao.getMyForYoungCollection(map);
        setImgList(list);
        return list;
    }

    @Override
    public void delForYoung(Map map) {
        younyDao.delForYoung(map);
    }


    //数据填充，特殊字符处理，图片列表处理，就是方法名字起的有问题
    private void setImgList(List<ForYounyEntity> list) throws UnsupportedEncodingException {
        for(ForYounyEntity forYounyEntity:list) {
            if(forYounyEntity.getTitleB()!=null)
                forYounyEntity.setTitle(new String(forYounyEntity.getTitleB(),"utf-8"));
            if(forYounyEntity.getNickNameB()!=null)
                forYounyEntity.setNackName(new String(forYounyEntity.getNickNameB(),"utf-8"));
            if(forYounyEntity.getFirstCommentUserNameB()!=null)
                forYounyEntity.setFirstCommentUserName(new String(forYounyEntity.getFirstCommentUserNameB(),"utf-8") );
            if(forYounyEntity.getFirstCommentContentB()!=null)
                forYounyEntity.setFirstCommentContent(new String(forYounyEntity.getFirstCommentContentB(),"utf-8"));
            List<ForYoungComment> forYoungCommentList=new ArrayList<>();
            ForYoungComment forYoungComment=new ForYoungComment();
            forYoungComment.setUserName(forYounyEntity.getFirstCommentUserName());
            forYoungComment.setContent(forYounyEntity.getFirstCommentContent());
            forYoungCommentList.add(forYoungComment);
            forYounyEntity.setForYoungCommentList(forYoungCommentList);
            //forYoungComment.setUserId(forYounyEntity.getFirstCommentUserId());
            if(forYounyEntity.getFirstCommentId()==null) forYounyEntity.setFirstCommentId(0);
            if(forYounyEntity.getFirstCommentContent()==null) forYounyEntity.setFirstCommentContent("");
            if(forYounyEntity.getFirstCommentUserId()==null) forYounyEntity.setFirstCommentUserId(0);
            if(forYounyEntity.getFirstCommentUserName()==null) forYounyEntity.setFirstCommentUserName("");
            try {
                if (forYounyEntity.getImgs() != null) {
                    String[] imgsList = forYounyEntity.getImgs().split(";");
                    String[] wh = forYounyEntity.getImgswh().split(";");
                    List<ImgEntiy> imgEntiys = new ArrayList<>();
                    for (int i = 0; i < imgsList.length; i++) {
                        ImgEntiy imgEntiy = new ImgEntiy();
                        String[] whh = wh[i].split(":");
                        imgEntiy.setImgUrl(imgsList[i]);
                        imgEntiy.setWidth(whh[0]);
                        imgEntiy.setHeigh(whh[1]);
                        imgEntiys.add(imgEntiy);
                    }
                    forYounyEntity.setImgList(imgEntiys);
                }
            } catch (Exception e) {

            }
        }
    }

    public void setCommentList(List<ForYoungComment> commentList) throws UnsupportedEncodingException {
        for(ForYoungComment forYoungComment:commentList){
            if(forYoungComment.getUserNameB()!=null)
                forYoungComment.setUserName(new String(forYoungComment.getUserNameB(),"utf-8"));
            if(forYoungComment.getContentB()!=null)
                forYoungComment.setContent(new String(forYoungComment.getContentB(),"utf-8"));
            if(forYoungComment.getFirstReplyUserNickNameB()!=null)
                forYoungComment.setFirstReplyUserNickName(new String(forYoungComment.getFirstReplyUserNickNameB(),"utf-8"));
            if(forYoungComment.getFirstReplyContentB()!=null)
                forYoungComment.setFirstReplyContent(new String(forYoungComment.getFirstReplyContentB(),"utf-8"));
            if(forYoungComment.getSecondReplyContentB()!=null)
                forYoungComment.setSecondReplyContent(new String(forYoungComment.getSecondReplyContentB(),"utf-8"));
            if(forYoungComment.getSecondReplyUserNickNameB()!=null)
                forYoungComment.setSecondReplyUserNickName(new String(forYoungComment.getSecondReplyUserNickNameB(),"utf-8"));
            List<ForYoungReply> forYoungReplies=new ArrayList<>();
            if(forYoungComment.getFirstReplyId()!=null){
                ForYoungReply forYoungReply=new ForYoungReply();
                if(forYoungComment.getFirstReplyContentB()!=null)
                    forYoungReply.setContent(new String(forYoungComment.getFirstReplyContentB(),"utf-8"));
                else
                    forYoungReply.setContent(forYoungComment.getFirstReplyContent());
                forYoungReply.setForYoungId(Math.toIntExact(forYoungComment.getForYoungId()));
                forYoungReply.setCommentId(forYoungComment.getId());
                forYoungReply.setId(forYoungComment.getFirstReplyId());
                forYoungReply.setCreateTime(forYoungComment.getFirstCreateTime());
                forYoungReply.setUserId(forYoungComment.getFirstReplyUserId());
               // forYoungReply.setUserHeadImage(forYoungComment.getF);
                // forYoungComment.setIsLike(younyDao.getReplyLIke(params));
                forYoungReply.setUserName(forYoungComment.getFirstReplyUserNickName());
                forYoungReplies.add(forYoungReply);
                if(forYoungComment.getSecondReplyUserId()!=null){
                    ForYoungReply forYoungReply1=new ForYoungReply();
                    if(forYoungComment.getSecondReplyContentB()!=null)
                        forYoungReply1.setContent(new String(forYoungComment.getSecondReplyContentB(),"utf-8"));
                   else
                       forYoungReply1.setContent(forYoungComment.getSecondReplyContent());
                    forYoungReply1.setForYoungId(Math.toIntExact(forYoungComment.getForYoungId()));
                    forYoungReply1.setCommentId(forYoungComment.getId());
                    forYoungReply1.setId(forYoungComment.getSecondReplyId());
                    // forYoungComment.setIsLike(younyDao.getReplyLIke(params));
                    forYoungReply1.setCreateTime(forYoungComment.getSecondCreateTime());
                    forYoungReply1.setUserId(forYoungComment.getSecondReplyUserId());
                    forYoungReply1.setUserName(forYoungComment.getSecondReplyUserNickName());
                    forYoungReplies.add(forYoungReply1);
                }
            }
            forYoungComment.setForYoungReplyList(forYoungReplies);
        }
    }

    public void setReglyList(List<ForYoungReply> reglyList) throws UnsupportedEncodingException {
        for(ForYoungReply f:reglyList){
            if(f.getUserNameB()!=null)
                f.setUserName(new String(f.getUserNameB(),"utf-8"));
            if(f.getContentB()!=null)
                f.setContent(new String(f.getContentB(),"utf-8"));
        }
    }
}
